#include "../TcpServer.hpp"
class EchoServer
{
private:
    TcpServer _server;
private:
    void OnEstablished(const ConnPtr &conn) { DLOG("CREATE CONNECTION: %p", conn.get()); }
    void OnClose(const ConnPtr &conn) { DLOG("CLOSE CONNECTION: %p", conn.get()); }
    void OnMessage(const ConnPtr &conn, Buffer *buf)
    {
        //echoServer的业务逻辑处理非常简单，只需要将对方发送的消息再转回发送给对方即可
        conn->Send(buf->ReadAddress(), buf->ReadableSize());
        buf->MoveReadIdx(buf->ReadableSize());
        // conn->Shutdown();
    }
public:
    EchoServer(uint16_t port) 
        :_server(port)
    {
        _server.SetThreadCount(2);
        _server.OpenInactiveRelease(10);
        _server.SetEstablishedCallBack(std::bind(&EchoServer::OnEstablished, this, std::placeholders::_1));
        _server.SetCloseCallBack(std::bind(&EchoServer::OnClose, this, std::placeholders::_1));
        _server.SetMessageCallBack(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
    }
    void Run() { _server.Run(); }
};
